package com.bookstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bookstore.common.Result;
import com.bookstore.entity.Order;
import com.bookstore.entity.OrderItem;
import com.bookstore.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
@Api(tags = "订单管理接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    @ApiOperation("创建订单")
    public Result<Integer> createOrder(
            @ApiParam("用户ID") @RequestParam Integer userId,
            @ApiParam("订单详情列表") @RequestBody List<OrderItem> orderItems,
            @ApiParam("收货地址") @RequestParam String shippingAddress,
            @ApiParam("支付方式") @RequestParam String paymentMethod) {
        return Result.success(orderService.createOrder(userId, orderItems, shippingAddress, paymentMethod));
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户订单列表")
    public Result<IPage<Order>> getUserOrders(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") int size) {
        return Result.success(orderService.getUserOrders(userId, page, size));
    }

    @GetMapping("/{orderId}/items")
    @ApiOperation("获取订单详情")
    public Result<List<OrderItem>> getOrderItems(
            @ApiParam("订单ID") @PathVariable Integer orderId) {
        return Result.success(orderService.getOrderItems(orderId));
    }

    @PutMapping("/{orderId}/status")
    @ApiOperation("更新订单状态")
    public Result<Boolean> updateOrderStatus(
            @ApiParam("订单ID") @PathVariable Integer orderId,
            @ApiParam("订单状态") @RequestParam String status) {
        return Result.success(orderService.updateOrderStatus(orderId, status));
    }

    @PutMapping("/{orderId}/cancel")
    @ApiOperation("取消订单")
    public Result<Boolean> cancelOrder(
            @ApiParam("订单ID") @PathVariable Integer orderId) {
        return Result.success(orderService.cancelOrder(orderId));
    }
} 